"""
 Django cheap-pages

 Methods to use when you just want to use the Django dispatcher and there 
 will be no extra business logic in your pages.
 
 In some cases flatpages is too flat, and store templates in DB is too 
 much hassle
 
 Where should I cache this?
 

 Jj (jjdelc@gmail.com)
  http://isgeek.net/ - 2008

"""
from os.path import join

from django.conf.urls.defaults import url
from django.views.generic.simple import direct_to_template

def page(destination, prefix='',
        template_dir='', 
        name_prefix='',
        default_extension='.html'):
    """
    Returns an url() object for the given name using direc_to_template.
    For "name":
        >>> url(^name/$,
        ...    direct_to_template,
        ...    {'template': 'name.html'},
        ...    name='name')

        can be expressed as:
        >>> page('name')

    Usage:
        urlpatterns = patterns('views',
            url(r'Regexp/$', view, name='str'),
            url(r'Regexp2/$', view, name='str'),
            page('page_name'),
        )
    """
    return url(r'^%s/$' % join(prefix, destination),
                direct_to_template,
                {'template': '%s%s' % (join(template_dir, destination),
                    default_extension)}, 
                name='%s%s' % (name_prefix, destination))

def build(prefix, destination_list, 
        template_dir='', # Where to look for the templates
        name_prefix='', # Prefix to use to name URLs, cheap_page (the 'cheap_')
        default_extension='.html' # are the pages .html, .csv, .whatever?
        ):
    """
    Builds a patterns object to render all the pages
    in destination_list.

    Usage:
        urlpatterns = patterns('', ...)
        urlpatterns += build('Regexp', ['page1', 'page2', ...])
    """
    urlpatterns = []
    for destination in destination_list:
        urlpatterns.append(page(
            destination, prefix, template_dir, name_prefix,
            default_extension))
    return urlpatterns

